﻿using SimpleConnection_UDP_WIN.utils;
using System;
using System.Text.RegularExpressions;

namespace SimpleConnection_UDP_WIN.extension_class.utils
{
    /// <summary>
    /// 消息解析类  用于解析消息中的各种数据
    /// </summary>
    internal class MessageParsingClass
    {
        /// <summary>
        /// 获取协议，不带://
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string getProtocol(string message)
        {

            int index = message.IndexOf("://");
            if (index != -1)
            {
                string result = message.Substring(0, index);
                return result;
            }

            return string.Empty;
        }
        /// <summary>
        /// 获取协议附带://
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string getProtocol2(string message)
        {

            int index = message.IndexOf("://");
            if (index != -1)
            {
                string result = message.Substring(0, index);
                return $"{result}://";
            }

            return string.Empty;
        }

        /// <summary>
        /// 获取不附带关键字的纯净字符串
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string getText(string message)
        {
            
            if (!string.IsNullOrEmpty(getProtocol2(message)))
            {
                message = message.Replace(getProtocol2(message), "");
            }

            if (!string.IsNullOrEmpty(getKey(message)))
            {
                message = message.Replace($"<key>{getKey(message)}</key>", "");

            }
            if (!string.IsNullOrEmpty(getString(message)))
            {
                message = message.Replace($"<string>{getKey(message)}</string>", "");

            }
            if (!string.IsNullOrEmpty(getPassword(message)))
            {
                message = message.Replace($"<pw>{getKey(message)}</pw>", "");

            }
            if (getNum(message)!=0)
            {
                message = message.Replace($"<num>{getKey(message)}</num>", "");

            }


            return message;
        }

        /// <summary>
        /// 获取<num>标签的数字
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static int getNum(string message)
        {

            try
            {
                return int.Parse(getTextMiddleContent(message, "<num>", "</num>"));
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// 获取 key
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string getKey(string message)
        {
            return getTextMiddleContent(message, "<key>", "</key>");
        }

        /// <summary>
        /// 获取string
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string getString(string message)
        {
            return getTextMiddleContent(message, "<string>", "</string>");
        }

        /// <summary>
        /// 获取密码，推荐使用 isAdmin 直接进行验证
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string getPassword(string message)
        {
            return getTextMiddleContent(message, "<pw>", "</pw>");
        }

        /// <summary>
        /// 验证是否携带了 密码 并 和本地设置的相同
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool isAdmin(string message)
        {
            //读取本地密码 比对 password参数，测试期间均为true

            if (RegistryUtils.getInt("check_password", 1) == 1)
            {
                // 读取注册表项的值
                string password = RegistryUtils.getString("password", "123456");

                Console.WriteLine($"本地密码：{password}  接收到的：{getPassword(message)}");
                if (getPassword(message).Equals(password))
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            else
            {
                return true;
            }
        }



        /// <summary>
        /// 获取 文本中间的内容
        /// </summary>
        /// <param name="input"></param>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        private static string getTextMiddleContent(string input, string left, string right)
        {
            string pattern = $@"{left}(.*?){right}";
            Match match = Regex.Match(input, pattern);
            if (match.Success)
            {
                string result = match.Groups[0].Value.Replace(left, @"").Replace(right, @"").Replace(@"\n", "\n");
                return result;
            }
            return string.Empty;
        }

    }
}
